<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04_webdriver_advanced</title>
<link rel="stylesheet" type="text/css" href="css/mystyle.css"/>
</head>
<body>


<div class="body">
            
  <div class="section" id="webdriver-advanced-usage">
<h1>WebDriver: Advanced Usage
<a class="headerlink" href="#webdriver-advanced-usage" title="Permalink to this headline">¶</a>
<a class="subTitle" href="http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp">online</a>
</h1>
<div class="section" id="explicit-and-implicit-waits">
<span id="explicit-and-implicit-waits-reference"></span><span id="chapter04-reference"></span><h2>Explicit and Implicit Waits<a class="headerlink" href="#explicit-and-implicit-waits" title="Permalink to this headline">¶</a></h2>
<p>Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.
You should choose to use Explicit Waits or Implicit Waits.</p>
<p>在继续下一步之前，等待正在执行自动任务执行一段时间。 您应该选择使用显式等待或隐式等待。</p>

<p>WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an
implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.</p>
<p>警告：不要混合隐式和显式等待。 这样做会导致无法预测的等待时间。 例如，设置10秒的隐式等待和15秒的显式等待，可能会在20秒后发生超时。</p>

<div class="section" id="explicit-waits">
<h3>Explicit Waits<a class="headerlink" href="#explicit-waits" title="Permalink to this headline">¶</a></h3>
<p>An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.
The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait.
There are some convenience methods provided that help you write code that will wait only as long as required.
WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.</p>
<p>明确的等待是你定义的代码，等待一定的条件发生，然后继续进行代码。 最糟糕的情况是Thread.sleep（），它将条件设置为等待的确切时间段。 有一些方便的方法可以帮助你编写只需要等待的代码。 WebDriverWait与ExpectedCondition结合是可以完成的一种方法。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">();</span>
<span class="n">driver</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"http://somedomain/url_that_delays_loading"</span><span class="o">);</span>
<span class="n">WebElement</span> <span class="n">myDynamicElement</span> <span class="o">=</span> <span class="o">(</span><span class="k">new</span> <span class="n">WebDriverWait</span><span class="o">(</span><span class="n">driver</span><span class="o">,</span> <span class="mi">10</span><span class="o">))</span>
  <span class="o">.</span><span class="na">until</span><span class="o">(</span><span class="n">ExpectedConditions</span><span class="o">.</span><span class="na">presenceOfElementLocated</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="s">"myDynamicElement"</span><span class="o">)));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="k">using</span> <span class="p">(</span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">())</span>
<span class="p">{</span>
    <span class="n">driver</span><span class="p">.</span><span class="n">Url</span> <span class="p">=</span> <span class="s">"http://somedomain/url_that_delays_loading"</span><span class="p">;</span>
    <span class="n">WebDriverWait</span> <span class="n">wait</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="n">TimeSpan</span><span class="p">.</span><span class="n">FromSeconds</span><span class="p">(</span><span class="m">10</span><span class="p">));</span>
    <span class="n">IWebElement</span> <span class="n">myDynamicElement</span> <span class="p">=</span> <span class="n">wait</span><span class="p">.</span><span class="n">Until</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;(</span><span class="n">d</span> <span class="p">=&gt;</span> <span class="n">d</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Id</span><span class="p">(</span><span class="s">"someDynamicElement"</span><span class="p">)));</span>
<span class="p">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support.ui</span> <span class="kn">import</span> <span class="n">WebDriverWait</span> <span class="c1"># available since 2.4.0</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support</span> <span class="kn">import</span> <span class="n">expected_conditions</span> <span class="k">as</span> <span class="n">EC</span> <span class="c1"># available since 2.26.0</span>

<span class="n">ff</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span>
<span class="n">ff</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://somedomain/url_that_delays_loading"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">element</span> <span class="o">=</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">ff</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">until</span><span class="p">(</span><span class="n">EC</span><span class="o">.</span><span class="n">presence_of_element_located</span><span class="p">((</span><span class="n">By</span><span class="o">.</span><span class="n">ID</span><span class="p">,</span> <span class="s2">"myDynamicElement"</span><span class="p">)))</span>
<span class="k">finally</span><span class="p">:</span>
    <span class="n">ff</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s1">'rubygems'</span> <span class="c1"># not required for ruby 1.9 or if you installed without gem</span>
<span class="nb">require</span> <span class="s1">'selenium-webdriver'</span>

<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span> <span class="s2">"http://somedomain/url_that_delays_loading"</span>

<span class="n">wait</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Wait</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:timeout</span> <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">)</span> <span class="c1"># seconds</span>
<span class="k">begin</span>
  <span class="n">element</span> <span class="o">=</span> <span class="n">wait</span><span class="o">.</span><span class="n">until</span> <span class="p">{</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:id</span> <span class="o">=&gt;</span> <span class="s2">"some-dynamic-element"</span><span class="p">)</span> <span class="p">}</span>
<span class="k">ensure</span>
  <span class="n">driver</span><span class="o">.</span><span class="n">quit</span>
<span class="k">end</span>
</pre></div>
</div>
<p>This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds.
WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.</p>
<p>在等待10秒之前抛出一个TimeoutException，或者如果它发现元素将在0-10秒内返回它。 WebDriverWait默认每500毫秒调用ExpectedCondition，直到成功返回。 ExpectedCondition函数类型的成功返回值是布尔值true或非空对象。</p>

<p>This example is also functionally equivalent to the first <a class="reference internal" href="#implicit-waits">Implicit Waits</a> example.</p>
<p>这个例子在功能上也等同于第一个Implicit Waits例子。</p>

<div class="section" id="expected-conditions">
<h4>Expected Conditions<a class="headerlink" href="#expected-conditions" title="Permalink to this headline">¶</a></h4>
<p>There are some common conditions that are frequently encountered when automating web browsers. Listed below are
a few examples for the usage of such conditions. The Java, C#, and Python bindings include convienence methods
so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.</p>
<p>自动化浏览器时，经常遇到一些常见的情况。 下面列出的是使用这些条件的几个例子。 Java，C＃和Python绑定包括convienence方法，因此您不必亲自编写ExpectedCondition类，也可以为其创建自己的实用程序包。</p>

<ul class="simple">
<li>Element is Clickable - it is Displayed and Enabled.</li>
</ul>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriverWait</span> <span class="n">wait</span> <span class="o">=</span> <span class="k">new</span> <span class="n">WebDriverWait</span><span class="o">(</span><span class="n">driver</span><span class="o">,</span> <span class="mi">10</span><span class="o">);</span>
<span class="n">WebElement</span> <span class="n">element</span> <span class="o">=</span> <span class="n">wait</span><span class="o">.</span><span class="na">until</span><span class="o">(</span><span class="n">ExpectedConditions</span><span class="o">.</span><span class="na">elementToBeClickable</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="s">"someid"</span><span class="o">)));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">WebDriverWait</span> <span class="n">wait</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="n">TimeSpan</span><span class="p">.</span><span class="n">FromSeconds</span><span class="p">(</span><span class="m">10</span><span class="p">));</span>
<span class="n">IWebElement</span> <span class="n">element</span> <span class="p">=</span> <span class="n">wait</span><span class="p">.</span><span class="n">Until</span><span class="p">(</span><span class="n">ExpectedConditions</span><span class="p">.</span><span class="n">ElementToBeClickable</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Id</span><span class="p">(</span><span class="s">"someid"</span><span class="p">)));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium.webdriver.support</span> <span class="kn">import</span> <span class="n">expected_conditions</span> <span class="k">as</span> <span class="n">EC</span>
<span class="n">wait</span> <span class="o">=</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">element</span> <span class="o">=</span> <span class="n">wait</span><span class="o">.</span><span class="n">until</span><span class="p">(</span><span class="n">EC</span><span class="o">.</span><span class="n">element_to_be_clickable</span><span class="p">((</span><span class="n">By</span><span class="o">.</span><span class="n">ID</span><span class="p">,</span><span class="s1">'someid'</span><span class="p">)))</span>
</pre></div>
</div>
<p>The ExpectedConditions package (<a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html">Java</a>)  (<a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html">Python</a>) (<a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm">.NET</a>) contains a set of predefined conditions to use with WebDriverWait.</p>
<p>ExpectedConditions包（Java）（Python）（.NET）包含一组与WebDriverWait一起使用的预定义条件。</p>

</div>
</div>
<div class="section" id="implicit-waits">
<h3>Implicit Waits<a class="headerlink" href="#implicit-waits" title="Permalink to this headline">¶</a></h3>
<p>An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.</p>
<p>一个隐含的等待就是告诉WebDriver在查找一个或多个元素（如果不是立即可用的）时轮询DOM一段时间。 默认设置为0.一旦设置，隐式等待就设置为WebDriver对象实例的生命周期。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">();</span>
<span class="n">driver</span><span class="o">.</span><span class="na">manage</span><span class="o">().</span><span class="na">timeouts</span><span class="o">().</span><span class="na">implicitlyWait</span><span class="o">(</span><span class="mi">10</span><span class="o">,</span> <span class="n">TimeUnit</span><span class="o">.</span><span class="na">SECONDS</span><span class="o">);</span>
<span class="n">driver</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"http://somedomain/url_that_delays_loading"</span><span class="o">);</span>
<span class="n">WebElement</span> <span class="n">myDynamicElement</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="s">"myDynamicElement"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="k">using</span> <span class="p">(</span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">())</span>
<span class="p">{</span>
    <span class="n">driver</span><span class="p">.</span><span class="n">Manage</span><span class="p">().</span><span class="n">Timeouts</span><span class="p">().</span><span class="n">ImplicitWait</span> <span class="p">=</span> <span class="n">TimeSpan</span><span class="p">.</span><span class="n">FromSeconds</span><span class="p">(</span><span class="m">10</span><span class="p">);</span>
    <span class="n">driver</span><span class="p">.</span><span class="n">Url</span> <span class="p">=</span> <span class="s">"http://somedomain/url_that_delays_loading"</span><span class="p">;</span>
    <span class="n">IWebElement</span> <span class="n">myDynamicElement</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Id</span><span class="p">(</span><span class="s">"someDynamicElement"</span><span class="p">));</span>
<span class="p">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>

<span class="n">ff</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span>
<span class="n">ff</span><span class="o">.</span><span class="n">implicitly_wait</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># seconds</span>
<span class="n">ff</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://somedomain/url_that_delays_loading"</span><span class="p">)</span>
<span class="n">myDynamicElement</span> <span class="o">=</span> <span class="n">ff</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s2">"myDynamicElement"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s1">'rubygems'</span> <span class="c1"># not required for ruby 1.9 or if you installed without gem</span>
<span class="nb">require</span> <span class="s1">'selenium-webdriver'</span>

<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span>
<span class="n">driver</span><span class="o">.</span><span class="n">manage</span><span class="o">.</span><span class="n">timeouts</span><span class="o">.</span><span class="n">implicit_wait</span> <span class="o">=</span> <span class="mi">10</span> <span class="c1"># seconds</span>

<span class="n">driver</span><span class="o">.</span><span class="n">get</span> <span class="s2">"http://somedomain/url_that_delays_loading"</span>
<span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:id</span> <span class="o">=&gt;</span> <span class="s2">"some-dynamic-element"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="remotewebdriver">
<h2>RemoteWebDriver<a class="headerlink" href="#remotewebdriver" title="Permalink to this headline">¶</a></h2>
<div class="section" id="taking-a-screenshot">
<h3>Taking a Screenshot<a class="headerlink" href="#taking-a-screenshot" title="Permalink to this headline">¶</a></h3>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">java.io.File</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.net.URL</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.openqa.selenium.OutputType</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.TakesScreenshot</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.WebDriver</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.remote.Augmenter</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.remote.DesiredCapabilities</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.remote.RemoteWebDriver</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Testing</span> <span class="o">{</span>
    
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">myTest</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
        <span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="o">(</span>
                                <span class="k">new</span> <span class="n">URL</span><span class="o">(</span><span class="s">"http://localhost:4444/wd/hub"</span><span class="o">),</span> 
                                <span class="n">DesiredCapabilities</span><span class="o">.</span><span class="na">firefox</span><span class="o">());</span>
        
        <span class="n">driver</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"http://www.google.com"</span><span class="o">);</span>
        
        <span class="c1">// RemoteWebDriver does not implement the TakesScreenshot class</span>
        <span class="c1">// if the driver does have the Capabilities to take a screenshot</span>
        <span class="c1">// then Augmenter will add the TakesScreenshot methods to the instance</span>
        <span class="n">WebDriver</span> <span class="n">augmentedDriver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Augmenter</span><span class="o">().</span><span class="na">augment</span><span class="o">(</span><span class="n">driver</span><span class="o">);</span>
        <span class="n">File</span> <span class="n">screenshot</span> <span class="o">=</span> <span class="o">((</span><span class="n">TakesScreenshot</span><span class="o">)</span><span class="n">augmentedDriver</span><span class="o">).</span>
                            <span class="n">getScreenshotAs</span><span class="o">(</span><span class="n">OutputType</span><span class="o">.</span><span class="na">FILE</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="c1">// Add this class to your code and use this instead of RemoteWebDriver</span>
<span class="c1">// You will then be able to cast it to ITakesScreenshot and call GetScreenshot</span>

<span class="k">public</span> <span class="k">class</span> <span class="nc">ScreenShotRemoteWebDriver</span> <span class="p">:</span> <span class="n">RemoteWebDriver</span><span class="p">,</span> <span class="n">ITakesScreenshot</span>
<span class="p">{</span>
       <span class="k">public</span> <span class="nf">ScreenShotRemoteWebDriver</span><span class="p">(</span><span class="n">Uri</span> <span class="n">RemoteAdress</span><span class="p">,</span> <span class="n">ICapabilities</span> <span class="n">capabilities</span><span class="p">)</span>
       <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">RemoteAdress</span><span class="p">,</span> <span class="n">capabilities</span><span class="p">)</span>
       <span class="p">{</span>           
       <span class="p">}</span>

       <span class="c1">/// &lt;summary&gt;</span>
       <span class="c1">/// Gets a &lt;see cref="Screenshot"/&gt; object representing the image of the page on the screen.</span>
       <span class="c1">/// &lt;/summary&gt;</span>
       <span class="c1">/// &lt;returns&gt;A &lt;see cref="Screenshot"/&gt; object containing the image.&lt;/returns&gt;</span>
       <span class="k">public</span> <span class="n">Screenshot</span> <span class="nf">GetScreenshot</span><span class="p">()</span>
       <span class="p">{</span>
           <span class="c1">// Get the screenshot as base64.</span>
           <span class="n">Response</span> <span class="n">screenshotResponse</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">Execute</span><span class="p">(</span><span class="n">DriverCommand</span><span class="p">.</span><span class="n">Screenshot</span><span class="p">,</span> <span class="k">null</span><span class="p">);</span>
           <span class="kt">string</span> <span class="n">base64</span> <span class="p">=</span> <span class="n">screenshotResponse</span><span class="p">.</span><span class="n">Value</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>

           <span class="c1">// ... and convert it.</span>
           <span class="k">return</span> <span class="k">new</span> <span class="nf">Screenshot</span><span class="p">(</span><span class="n">base64</span><span class="p">);</span>
       <span class="p">}</span>
<span class="p">}</span>


<span class="c1">// And then the usage would be:</span>

<span class="n">ScreenShotRemoteWebDriver</span> <span class="n">webDriver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ScreenShotRemoteWebDriver</span><span class="p">(</span><span class="k">new</span> <span class="n">Uri</span><span class="p">(</span><span class="s">"http://127.0.0.1:4444/wd/hub"</span><span class="p">),</span> <span class="n">DesiredCapabilities</span><span class="p">.</span><span class="n">Firefox</span><span class="p">());</span>
<span class="c1">// ... do stuff with webDriver</span>
<span class="n">Screenshot</span> <span class="n">ss</span> <span class="p">=</span> <span class="p">((</span><span class="n">ITakesScreenshot</span><span class="p">)</span><span class="n">webDriver</span><span class="p">).</span><span class="n">GetScreenshot</span><span class="p">();</span>
<span class="kt">string</span> <span class="n">screenshot</span> <span class="p">=</span> <span class="n">ss</span><span class="p">.</span><span class="n">AsBase64EncodedString</span><span class="p">;</span>
<span class="kt">byte</span><span class="p">[]</span> <span class="n">screenshotAsByteArray</span> <span class="p">=</span> <span class="n">ss</span><span class="p">.</span><span class="n">AsByteArray</span><span class="p">;</span>
<span class="n">ss</span><span class="p">.</span><span class="n">SaveAsFile</span><span class="p">(</span><span class="n">activeDir</span> <span class="p">+</span> <span class="n">TestSuiteName</span> <span class="p">+</span> <span class="s">"//"</span> <span class="p">+</span> <span class="n">FileNanme</span> <span class="p">+</span> <span class="n">imageFormat</span><span class="p">,</span> <span class="n">ImageFormat</span><span class="p">.</span><span class="n">Jpeg</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>

<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">DesiredCapabilities</span><span class="o">.</span><span class="n">FIREFOX</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://www.google.com"</span><span class="p">)</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get_screenshot_as_file</span><span class="p">(</span><span class="s1">'/Screenshots/google.png'</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s1">'rubygems'</span>
<span class="nb">require</span> <span class="s1">'selenium-webdriver'</span>

<span class="k">begin</span>
  <span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:remote</span><span class="p">,</span> <span class="ss">:url</span> <span class="o">=&gt;</span> <span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="ss">:desired_capabilities</span> <span class="o">=&gt;</span> <span class="ss">:firefox</span>
  <span class="n">driver</span><span class="o">.</span><span class="n">get</span> <span class="s2">"http://www.google.com"</span>
  <span class="n">driver</span><span class="o">.</span><span class="n">save_screenshot</span> <span class="s2">"/Screenshots/google.png"</span>
<span class="k">ensure</span>
  <span class="n">driver</span><span class="o">.</span><span class="n">quit</span>
<span class="k">end</span>
</pre></div>
</div>
</div>
<div class="section" id="using-a-firefoxprofile">
<h3>Using a FirefoxProfile<a class="headerlink" href="#using-a-firefoxprofile" title="Permalink to this headline">¶</a></h3>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">FirefoxProfile</span> <span class="n">fp</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="o">();</span>
<span class="c1">// set something on the profile...</span>
<span class="n">DesiredCapabilities</span> <span class="n">dc</span> <span class="o">=</span> <span class="n">DesiredCapabilities</span><span class="o">.</span><span class="na">firefox</span><span class="o">();</span>
<span class="n">dc</span><span class="o">.</span><span class="na">setCapability</span><span class="o">(</span><span class="n">FirefoxDriver</span><span class="o">.</span><span class="na">PROFILE</span><span class="o">,</span> <span class="n">fp</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="o">(</span><span class="n">dc</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">FirefoxProfile</span> <span class="n">fp</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="p">();</span>
<span class="c1">// set something on the profile...</span>
<span class="n">DesiredCapabilities</span> <span class="n">dc</span> <span class="p">=</span> <span class="n">DesiredCapabilities</span><span class="p">.</span><span class="n">Firefox</span><span class="p">();</span>
<span class="n">dc</span><span class="p">.</span><span class="n">SetCapability</span><span class="p">(</span><span class="n">FirefoxDriver</span><span class="p">.</span><span class="n">ProfileCapabilityName</span><span class="p">,</span> <span class="n">fp</span><span class="p">);</span>
<span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="p">(</span><span class="n">dc</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
<span class="n">fp</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">FirefoxProfile</span><span class="p">()</span>
<span class="c1"># set something on the profile...</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="n">desired_capabilities</span><span class="o">=</span><span class="n">webdriver</span><span class="o">.</span><span class="n">DesiredCapabilities</span><span class="o">.</span><span class="n">FIREFOX</span><span class="p">,</span> <span class="n">browser_profile</span><span class="o">=</span><span class="n">fp</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-chromeoptions">
<h3>Using ChromeOptions<a class="headerlink" href="#using-chromeoptions" title="Permalink to this headline">¶</a></h3>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">ChromeOptions</span> <span class="n">options</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ChromeOptions</span><span class="o">();</span>
<span class="c1">// set some options</span>
<span class="n">DesiredCapabilities</span> <span class="n">dc</span> <span class="o">=</span> <span class="n">DesiredCapabilities</span><span class="o">.</span><span class="na">chrome</span><span class="o">();</span>
<span class="n">dc</span><span class="o">.</span><span class="na">setCapability</span><span class="o">(</span><span class="n">ChromeOptions</span><span class="o">.</span><span class="na">CAPABILITY</span><span class="o">,</span> <span class="n">options</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="o">(</span><span class="n">dc</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">options</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ChromeOptions</span><span class="p">();</span>
<span class="c1">// set some options</span>
<span class="n">DesiredCapabilities</span> <span class="n">dc</span> <span class="p">=</span> <span class="n">DesiredCapabilities</span><span class="p">.</span><span class="n">Chrome</span><span class="p">();</span>
<span class="n">dc</span><span class="p">.</span><span class="n">SetCapability</span><span class="p">(</span><span class="n">ChromeOptions</span><span class="p">.</span><span class="n">Capability</span><span class="p">,</span> <span class="n">options</span><span class="p">);</span>
<span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="p">(</span><span class="n">dc</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
<span class="n">options</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">ChromeOptions</span><span class="p">()</span>
<span class="c1"># set some options</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="n">desired_capabilities</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">to_capabilities</span><span class="p">())</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="advanceduserinteractions">
<h2>AdvancedUserInteractions<a class="headerlink" href="#advanceduserinteractions" title="Permalink to this headline">¶</a></h2>
<p>The Actions class(es) allow you to build a Chain of Actions and perform them.
There are too many possible combinations to count. Below are a few of the common
interactions that you may want to use. For a full list of actions please refer to
the API docs <a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/interactions/Actions.html">Java</a>
<a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/dotnet/html/AllMembers_T_OpenQA_Selenium_Interactions_Actions.htm">C#</a>
<a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/ActionBuilder.html">Ruby</a>
<a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html">Python</a></p>
<p>动作类允许你建立一个动作链并执行它们。 有太多可能的组合数。 以下是您可能要使用的一些常见交互。 有关完整的操作列表，请参阅API文档Java C＃Ruby Python</p>

<p>The Advanced User Interactions require native events to be enabled. Here’s a table
of the current support Matrix for native events:</p>
<p>高级用户交互需要启用本地事件。 以下是本地事件支持矩阵的表格：</p>

<table border="1" class="docutils">
<colgroup>
<col width="19%">
<col width="3%">
<col width="3%">
<col width="3%">
<col width="3%">
<col width="7%">
<col width="7%">
<col width="14%">
<col width="12%">
<col width="11%">
<col width="6%">
<col width="8%">
<col width="3%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">platform</th>
<th class="head">IE6</th>
<th class="head">IE7</th>
<th class="head">IE8</th>
<th class="head">IE9</th>
<th class="head">FF3.6</th>
<th class="head">FF10+</th>
<th class="head">Chrome stable</th>
<th class="head">Chrome beta</th>
<th class="head">Chrome dev</th>
<th class="head">Opera</th>
<th class="head">Android</th>
<th class="head">iOS</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Windows XP</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>n/a</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>?</td>
<td>Y <a class="footnote-reference" href="#id9" id="id3">[1]</a></td>
<td>n/a</td>
</tr>
<tr class="row-odd"><td>Windows 7</td>
<td>n/a</td>
<td>n/a</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>?</td>
<td>Y <a class="footnote-reference" href="#id9" id="id4">[1]</a></td>
<td>n/a</td>
</tr>
<tr class="row-even"><td>Linux (Ubuntu)</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>Y <a class="footnote-reference" href="#id10" id="id5">[2]</a></td>
<td>Y <a class="footnote-reference" href="#id10" id="id6">[2]</a></td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>?</td>
<td>Y <a class="footnote-reference" href="#id9" id="id7">[1]</a></td>
<td>n/a</td>
</tr>
<tr class="row-odd"><td>Mac OSX</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>N</td>
<td>N</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>?</td>
<td>Y <a class="footnote-reference" href="#id9" id="id8">[1]</a></td>
<td>N</td>
</tr>
<tr class="row-even"><td>Mobile Device</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>?</td>
<td>n/a</td>
<td>n/a</td>
<td>n/a</td>
<td>?</td>
<td>Y</td>
<td>N</td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id9" rules="none">
<colgroup><col class="label"><col></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td><em>(<a class="fn-backref" href="#id3">1</a>, <a class="fn-backref" href="#id4">2</a>, <a class="fn-backref" href="#id7">3</a>, <a class="fn-backref" href="#id8">4</a>)</em> Using the emulator</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id10" rules="none">
<colgroup><col class="label"><col></colgroup>
<tbody valign="top">
<tr><td class="label">[2]</td><td><em>(<a class="fn-backref" href="#id5">1</a>, <a class="fn-backref" href="#id6">2</a>)</em> With explicitly enabling native events</td></tr>
</tbody>
</table>
</div>
<div class="section" id="browser-startup-manipulation">
<h2>Browser Startup Manipulation<a class="headerlink" href="#browser-startup-manipulation" title="Permalink to this headline">¶</a></h2>
<p>Todo</p>
<p>Topics to be included: <span class="subA">要包括的主题：</span></p>
<ul class="simple">
<li>restoring cookies <span class="subA">恢复Cookie</span></li>
<li>changing firefox profile <span class="subA">改变Firefox的个人资料</span></li>
<li>running browsers with plugins <span class="subA">运行带有插件的浏览器</span></li>
</ul>
<div class="section" id="using-a-proxy">
<h3>Using a Proxy<a class="headerlink" href="#using-a-proxy" title="Permalink to this headline">¶</a></h3>
<div class="section" id="internet-explorer">
<h4>Internet Explorer<a class="headerlink" href="#internet-explorer" title="Permalink to this headline">¶</a></h4>
<p>The easiest and recommended way is to manually set the proxy on the machine that will be running the test.
If that is not possible or you want your test to run with a different configuration or proxy,
then you can use the following technique that uses a Capababilities object. This temporarily changes the system’s
proxy settings and changes them back to the original state when done.</p>
<p>最简单和推荐的方法是在将要运行测试的机器上手动设置代理。 如果这是不可能的，或者您希望测试使用不同的配置或代理运行，那么您可以使用以下使用Capababilities对象的技术。 这会暂时更改系统的代理设置，并在完成后将其更改回原始状态。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">String</span> <span class="n">PROXY</span> <span class="o">=</span> <span class="s">"localhost:8080"</span><span class="o">;</span>

<span class="n">org</span><span class="o">.</span><span class="na">openqa</span><span class="o">.</span><span class="na">selenium</span><span class="o">.</span><span class="na">Proxy</span> <span class="n">proxy</span> <span class="o">=</span> <span class="k">new</span> <span class="n">org</span><span class="o">.</span><span class="na">openqa</span><span class="o">.</span><span class="na">selenium</span><span class="o">.</span><span class="na">Proxy</span><span class="o">();</span>
<span class="n">proxy</span><span class="o">.</span><span class="na">setHttpProxy</span><span class="o">(</span><span class="n">PROXY</span><span class="o">)</span>
     <span class="o">.</span><span class="na">setFtpProxy</span><span class="o">(</span><span class="n">PROXY</span><span class="o">)</span>
     <span class="o">.</span><span class="na">setSslProxy</span><span class="o">(</span><span class="n">PROXY</span><span class="o">);</span>
<span class="n">DesiredCapabilities</span> <span class="n">cap</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DesiredCapabilities</span><span class="o">();</span>
<span class="n">cap</span><span class="o">.</span><span class="na">setCapability</span><span class="o">(</span><span class="n">CapabilityType</span><span class="o">.</span><span class="na">PROXY</span><span class="o">,</span> <span class="n">proxy</span><span class="o">);</span>

<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InternetExplorerDriver</span><span class="o">(</span><span class="n">cap</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>

<span class="n">PROXY</span> <span class="o">=</span> <span class="s2">"localhost:8080"</span>

<span class="c1"># Create a copy of desired capabilities object.</span>
<span class="n">desired_capabilities</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">DesiredCapabilities</span><span class="o">.</span><span class="n">INTERNETEXPLORER</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="c1"># Change the proxy properties of that copy.</span>
<span class="n">desired_capabilities</span><span class="p">[</span><span class="s1">'proxy'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">"httpProxy"</span><span class="p">:</span><span class="n">PROXY</span><span class="p">,</span>
    <span class="s2">"ftpProxy"</span><span class="p">:</span><span class="n">PROXY</span><span class="p">,</span>
    <span class="s2">"sslProxy"</span><span class="p">:</span><span class="n">PROXY</span><span class="p">,</span>
    <span class="s2">"noProxy"</span><span class="p">:</span><span class="bp">None</span><span class="p">,</span>
    <span class="s2">"proxyType"</span><span class="p">:</span><span class="s2">"MANUAL"</span><span class="p">,</span>
    <span class="s2">"class"</span><span class="p">:</span><span class="s2">"org.openqa.selenium.Proxy"</span><span class="p">,</span>
    <span class="s2">"autodetect"</span><span class="p">:</span><span class="bp">False</span>
<span class="p">}</span>

<span class="c1"># you have to use remote, otherwise you'll have to code it yourself in python to </span>
<span class="c1"># dynamically changing the system proxy preferences</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="n">desired_capabilities</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s1">'rubygems'</span> 
<span class="nb">require</span> <span class="s1">'selenium-webdriver'</span>

<span class="no">PROXY</span> <span class="o">=</span> <span class="s2">"localhost:8080"</span>

<span class="n">proxy</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Proxy</span><span class="o">.</span><span class="n">new</span><span class="p">(</span>
  <span class="ss">:http</span>     <span class="o">=&gt;</span> <span class="no">PROXY</span><span class="p">,</span>
  <span class="ss">:ftp</span>      <span class="o">=&gt;</span> <span class="no">PROXY</span><span class="p">,</span>
  <span class="ss">:ssl</span>      <span class="o">=&gt;</span> <span class="no">PROXY</span>
<span class="p">)</span>

<span class="n">caps</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Remote</span><span class="o">::</span><span class="no">Capabilities</span><span class="o">.</span><span class="n">ie</span><span class="p">(</span><span class="ss">:proxy</span> <span class="o">=&gt;</span> <span class="n">proxy</span><span class="p">)</span>

<span class="c1"># you have to use remote, otherwise you'll have to code it yourself in ruby to </span>
<span class="c1"># dynamically changing the system proxy preferences</span>
<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:remote</span><span class="p">,</span> <span class="ss">:url</span> <span class="o">=&gt;</span> <span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="ss">:desired_capabilities</span> <span class="o">=&gt;</span> <span class="n">caps</span>
</pre></div>
</div>
</div>
<div class="section" id="chrome">
<h4>Chrome<a class="headerlink" href="#chrome" title="Permalink to this headline">¶</a></h4>
<p>Is basically the same as internet explorer. It uses the same configuration on the machine as IE does (on windows).
On Mac it uses the System Preference -&gt; Network settings. On Linux it uses (on Ubuntu) System &gt; Preferences &gt;
Network Proxy Preferences (Alternatively in “/etc/environment” set http_proxy).
As of this writing it is unknown how to set the proxy programmatically.</p>
<p>基本上和Internet Explorer一样。 它在机器上使用与IE相同的配置（在Windows上）。 在Mac上，它使用系统首选项 - >网络设置。 在Linux上，它使用（在Ubuntu上）系统>首选项>网络代理首选项（或者在“/ etc / environment”中设置http_proxy）。 在撰写本文时，如何以编程方式设置代理是未知的。</p>

</div>
<div class="section" id="firefox-up-to-version-47-0-1">
<h4>Firefox up to version 47.0.1<a class="headerlink" href="#firefox-up-to-version-47-0-1" title="Permalink to this headline">¶</a></h4>
<p>Firefox maintains its proxy configuration in a profile. You can preset the
proxy in a profile and use that Firefox Profile or you can set it on profile
that is created on the fly as is shown in the following example -
deprecated, no longer working with GeckoDriver.</p>
<p>Firefox在配置文件中维护其代理配置。 您可以在配置文件中预设代理并使用该Firefox配置文件，或者您可以在动态创建的配置文件上设置它，如以下示例所示 - 不建议使用，不再使用GeckoDriver。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">String</span> <span class="n">PROXY</span> <span class="o">=</span> <span class="s">"localhost:8080"</span><span class="o">;</span>

<span class="n">org</span><span class="o">.</span><span class="na">openqa</span><span class="o">.</span><span class="na">selenium</span><span class="o">.</span><span class="na">Proxy</span> <span class="n">proxy</span> <span class="o">=</span> <span class="k">new</span> <span class="n">org</span><span class="o">.</span><span class="na">openqa</span><span class="o">.</span><span class="na">selenium</span><span class="o">.</span><span class="na">Proxy</span><span class="o">();</span>
<span class="n">proxy</span><span class="o">.</span><span class="na">setHttpProxy</span><span class="o">(</span><span class="n">PROXY</span><span class="o">)</span>
     <span class="o">.</span><span class="na">setFtpProxy</span><span class="o">(</span><span class="n">PROXY</span><span class="o">)</span>
     <span class="o">.</span><span class="na">setSslProxy</span><span class="o">(</span><span class="n">PROXY</span><span class="o">);</span>
<span class="n">DesiredCapabilities</span> <span class="n">cap</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DesiredCapabilities</span><span class="o">();</span>
<span class="n">cap</span><span class="o">.</span><span class="na">setCapability</span><span class="o">(</span><span class="n">CapabilityType</span><span class="o">.</span><span class="na">PROXY</span><span class="o">,</span> <span class="n">proxy</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">(</span><span class="n">cap</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.common.proxy</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">myProxy</span> <span class="o">=</span> <span class="s2">"host:8080"</span>

<span class="n">proxy</span> <span class="o">=</span> <span class="n">Proxy</span><span class="p">({</span>
    <span class="s1">'proxyType'</span><span class="p">:</span> <span class="n">ProxyType</span><span class="o">.</span><span class="n">MANUAL</span><span class="p">,</span>
    <span class="s1">'httpProxy'</span><span class="p">:</span> <span class="n">myProxy</span><span class="p">,</span>
    <span class="s1">'ftpProxy'</span><span class="p">:</span> <span class="n">myProxy</span><span class="p">,</span>
    <span class="s1">'sslProxy'</span><span class="p">:</span> <span class="n">myProxy</span><span class="p">,</span>
    <span class="s1">'noProxy'</span><span class="p">:</span> <span class="s1">''</span> <span class="c1"># set this value as desired</span>
    <span class="p">})</span>

<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">(</span><span class="n">proxy</span><span class="o">=</span><span class="n">proxy</span><span class="p">)</span>

<span class="c1"># for remote</span>
<span class="n">caps</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">DesiredCapabilities</span><span class="o">.</span><span class="n">FIREFOX</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">proxy</span><span class="o">.</span><span class="n">add_to_capabilities</span><span class="p">(</span><span class="n">caps</span><span class="p">)</span>

<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="n">desired_capabilities</span><span class="o">=</span><span class="n">caps</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s1">'rubygems'</span>
<span class="nb">require</span> <span class="s1">'selenium-webdriver'</span>

<span class="no">PROXY</span> <span class="o">=</span> <span class="s1">'localhost:8087'</span>

<span class="n">profile</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Firefox</span><span class="o">::</span><span class="no">Profile</span><span class="o">.</span><span class="n">new</span>
<span class="n">profile</span><span class="o">.</span><span class="n">proxy</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Proxy</span><span class="o">.</span><span class="n">new</span><span class="p">(</span>
  <span class="ss">:http</span>     <span class="o">=&gt;</span> <span class="no">PROXY</span><span class="p">,</span>
  <span class="ss">:ftp</span>      <span class="o">=&gt;</span> <span class="no">PROXY</span><span class="p">,</span>
  <span class="ss">:ssl</span>      <span class="o">=&gt;</span> <span class="no">PROXY</span>
<span class="p">)</span>

<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span><span class="p">,</span> <span class="ss">:profile</span> <span class="o">=&gt;</span> <span class="n">profile</span>
</pre></div>
</div>
</div>
<div class="section" id="firefox-version-48-and-newer-geckodriver">
<h4>Firefox version 48 and newer - GeckoDriver<a class="headerlink" href="#firefox-version-48-and-newer-geckodriver" title="Permalink to this headline">¶</a></h4>
<p>Firefox maintains its proxy configuration in a profile. You can preset the
proxy in a profile and use that Firefox Profile or you can set it on profile
that is created on the fly as is shown in the following example. With
GeckoDriver the proxy has to be passed through the required capabilities.</p>
<p>Firefox在配置文件中维护其代理配置。 您可以在配置文件中预设代理并使用该Firefox配置文件，或者您可以将其设置在即时创建的配置文件中，如以下示例所示。 使用GeckoDriver代理必须通过所需的功能。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">String</span> <span class="n">PROXY</span> <span class="o">=</span> <span class="s">"localhost"</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">PORT</span> <span class="o">=</span> <span class="mi">8080</span><span class="o">;</span>

<span class="n">com</span><span class="o">.</span><span class="na">google</span><span class="o">.</span><span class="na">gson</span><span class="o">.</span><span class="na">JsonObject</span> <span class="n">json</span> <span class="o">=</span> <span class="k">new</span> <span class="n">com</span><span class="o">.</span><span class="na">google</span><span class="o">.</span><span class="na">gson</span><span class="o">.</span><span class="na">JsonObject</span><span class="o">();</span>
<span class="n">json</span><span class="o">.</span><span class="na">addProperty</span><span class="o">(</span><span class="s">"proxyType"</span><span class="o">,</span> <span class="s">"MANUAL"</span><span class="o">);</span>
<span class="n">json</span><span class="o">.</span><span class="na">addProperty</span><span class="o">(</span><span class="s">"httpProxy"</span><span class="o">,</span> <span class="n">PROXY</span><span class="o">);</span>
<span class="n">json</span><span class="o">.</span><span class="na">addProperty</span><span class="o">(</span><span class="s">"httpProxyPort"</span><span class="o">,</span> <span class="n">PORT</span><span class="o">);</span>
<span class="n">json</span><span class="o">.</span><span class="na">addProperty</span><span class="o">(</span><span class="s">"sslProxy"</span><span class="o">,</span> <span class="n">PROXY</span><span class="o">);</span>
<span class="n">json</span><span class="o">.</span><span class="na">addProperty</span><span class="o">(</span><span class="s">"sslProxyPort"</span><span class="o">,</span> <span class="n">PORT</span><span class="o">);</span>

<span class="n">DesiredCapabilities</span> <span class="n">cap</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DesiredCapabilities</span><span class="o">();</span>
<span class="n">cap</span><span class="o">.</span><span class="na">setCapability</span><span class="o">(</span><span class="s">"proxy"</span><span class="o">,</span> <span class="n">json</span><span class="o">);</span>

<span class="n">GeckoDriverService</span> <span class="n">service</span> <span class="o">=</span><span class="k">new</span> <span class="n">GeckoDriverService</span><span class="o">.</span><span class="na">Builder</span><span class="o">(</span><span class="n">firefoxBinary</span><span class="o">)</span>
  <span class="o">.</span><span class="na">usingDriverExecutable</span><span class="o">(</span><span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="s">"path to geckodriver"</span><span class="o">))</span>
  <span class="o">.</span><span class="na">usingAnyFreePort</span><span class="o">()</span>
  <span class="o">.</span><span class="na">usingAnyFreePort</span><span class="o">()</span>
  <span class="o">.</span><span class="na">build</span><span class="o">();</span>
<span class="n">service</span><span class="o">.</span><span class="na">start</span><span class="o">();</span>

<span class="c1">// GeckoDriver currently needs the Proxy set in RequiredCapabilities</span>
<span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">(</span><span class="n">service</span><span class="o">,</span> <span class="n">cap</span><span class="o">,</span> <span class="n">cap</span><span class="o">);</span>
</pre></div>
</div>
</div>
<div class="section" id="opera">
<h4>Opera<a class="headerlink" href="#opera" title="Permalink to this headline">¶</a></h4>
<p>Todo</p>
</div>
</div>
</div>
<div class="section" id="html5">
<h2>HTML5<a class="headerlink" href="#html5" title="Permalink to this headline">¶</a></h2>
<p>Todo</p>
</div>
<div class="section" id="parallelizing-your-test-runs">
<h2>Parallelizing Your Test Runs<a class="headerlink" href="#parallelizing-your-test-runs" title="Permalink to this headline">¶</a></h2>
<p>Todo</p>
</div>
</div>


          </div>



</body>
</html>